package com.bj58.wuxian.lucene.utils;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryTermScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

public class LuceneUtil {

	public static Directory getDirectory(String path) {
		Directory directory = null;
		try {
			directory = FSDirectory.open(Paths.get(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return directory;
	}

	public static Directory getRAMDirectory() {
		Directory directory = new RAMDirectory();
		return directory;
	}
	
	

	public static DirectoryReader getDirectoryReader(Directory directory) {
		DirectoryReader reader = null;
		try {
			reader = DirectoryReader.open(directory);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reader;
	}

	public static IndexSearcher getIndexSearcher(DirectoryReader reader) {

		IndexSearcher indexSearcher = new IndexSearcher(reader);
		return indexSearcher;
	}

	public static IndexWriter getIndexWriter(Directory directory, Analyzer analyzer) {

		IndexWriter iwriter = null;
		try {
			IndexWriterConfig config = new IndexWriterConfig(analyzer);
			config.setOpenMode(OpenMode.CREATE_OR_APPEND);
			// Sort sort=new Sort(new SortField("content", Type.STRING));
			// config.setIndexSort(sort);//排序
			 config.setCommitOnClose(true);
			// config.setMergeScheduler(new ConcurrentMergeScheduler());
			// config.setIndexDeletionPolicy(new
			// SnapshotDeletionPolicy(NoDeletionPolicy.INSTANCE));
			iwriter = new IndexWriter(directory, config);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return iwriter;
	}

	
	public static void  close(IndexWriter indexWriter,Directory directory){
		if(indexWriter!=null){
			try {
				indexWriter.close();
			} catch (IOException e) {
				indexWriter=null;
			}
		}
		if(directory!=null){
			try {
				directory.close();
			} catch (IOException e) {
				directory=null;
			}
		}
	}
	
	public static void  close(DirectoryReader reader,Directory directory){
		if(reader!=null){
			try {
				reader.close();
			} catch (IOException e) {
				reader=null;
			}
		}
		if(directory!=null){
			try {
				directory.close();
			} catch (IOException e) {
				directory=null;
			}
		}
	}
	
	/**
	 * 高亮标签
	 * @param query
	 * @param fieldName
	 * @return
	 */
	public static Highlighter getHighlighter(Query query, String fieldName) {
		Formatter formatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
		Scorer fragmentScorer = new QueryTermScorer(query, fieldName);
		Highlighter highlighter = new Highlighter(formatter, fragmentScorer);
		highlighter.setTextFragmenter(new SimpleFragmenter(200));
		return highlighter;
	}


}
